home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Modules / main.c < prev    next >
C/C++ Source or Header  |  1999-04-27  |  12KB  |  475 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Python interpreter main program */
  33.  
  34. #include "Python.h"
  35. #include "osdefs.h"
  36.  
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. #endif
  40.  
  41. #ifdef MS_WINDOWS
  42. #include <fcntl.h>
  43. #endif
  44.  
  45. #if defined(PYOS_OS2) || defined(MS_WINDOWS)
  46. #define PYTHONHOMEHELP "<prefix>\\lib"
  47. #else
  48. #define PYTHONHOMEHELP "<prefix>/python1.5"
  49. #endif
  50.  
  51. /* Interface to getopt(): */
  52. extern int optind;
  53. extern char *optarg;
  54. extern int getopt(); /* PROTO((int, char **, char *)); -- not standardized */
  55.  
  56.  
  57. /* For Py_GetArgcArgv(); set by main() */
  58. static char **orig_argv;
  59. static int  orig_argc;
  60.  
  61. /* Short usage message (with %s for argv0) */
  62. static char *usage_line =
  63. "usage: %s [option] ... [-c cmd | file | -] [arg] ...\n";
  64.  
  65. /* Long usage message, split into parts < 512 bytes */
  66. static char *usage_top = "\
  67. Options and arguments (and corresponding environment variables):\n\
  68. -d     : debug output from parser (also PYTHONDEBUG=x)\n\
  69. -i     : inspect interactively after running script, (also PYTHONINSPECT=x)\n\
  70.          and force prompts, even if stdin does not appear to be a terminal\n\
  71. -O     : optimize generated bytecode (a tad; also PYTHONOPTIMIZE=x)\n\
  72. -OO    : remove doc-strings in addition to the -O optimizations\n\
  73. -S     : don't imply 'import site' on initialization\n\
  74. -t     : issue warnings about inconsistent tab usage (-tt: issue errors)\n\
  75. ";
  76. static char *usage_mid = "\
  77. -u     : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)\n\
  78. -v     : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
  79. -x     : skip first line of source, allowing use of non-Unix forms of #!cmd\n\
  80. -X     : disable class based built-in exceptions\n\
  81. -c cmd : program passed in as string (terminates option list)\n\
  82. file   : program read from script file\n\
  83. -      : program read from stdin (default; interactive mode if a tty)\n\
  84. ";
  85. static char *usage_bot = "\
  86. arg ...: arguments passed to program in sys.argv[1:]\n\
  87. Other environment variables:\n\
  88. PYTHONSTARTUP: file executed on interactive startup (no default)\n\
  89. PYTHONPATH   : '%c'-separated list of directories prefixed to the\n\
  90.                default module search path.  The result is sys.path.\n\
  91. PYTHONHOME   : alternate <prefix> directory (or <prefix>%c<exec_prefix>).\n\
  92.                The default module search path uses %s.\n\
  93. ";
  94.  
  95.  
  96. #ifdef __SASC
  97. extern char __stdiowin[] = "CON:0/12/640/200/Python";
  98. extern char __stdiov37[] = "/AUTO";
  99. #endif
  100.  
  101. #ifdef _AMIGA
  102. #include <proto/dos.h>
  103. #include <proto/exec.h>
  104. #include "patchlevel.h"
  105. /* AmigaDOS version string */
  106. static const char ver[] = "$VER: Python " PATCHLEVEL " " __AMIGADATE__
  107. #ifdef AMITCP
  108.  " AmiTCP";
  109. #else
  110. #ifdef INET225
  111.  " I-Net225";
  112. #else
  113.  "";
  114. #endif /* INET225 */
  115. #endif /* AMITCP */
  116. #endif /* AMIGA */
  117.  
  118. #ifdef INET225
  119. #include <proto/socket.h>
  120.  
  121. struct Library *SockBase = NULL;
  122.  
  123. int checksocketlib(void)
  124. {
  125.     if(!SockBase)
  126.     {
  127.         if(SockBase=OpenLibrary("inet:libs/socket.library",4))
  128.         {
  129.             setup_sockets(FD_SETSIZE, &errno);
  130.         }
  131.         else
  132.         {
  133.             PyErr_SetString(PyExc_SystemError, "Couldn't open inet:libs/socket.library V4+ (I-Net225 started?)");
  134.             return 0;
  135.         }
  136.     }
  137.     return 1;
  138. }
  139.  
  140. int checkusergrouplib(void)
  141. {
  142.     return checksocketlib();
  143. }
  144.  
  145. #endif /* INET225 */
  146.  
  147. #ifdef AMITCP
  148. #include <proto/socket.h>
  149. #include <amitcp/socketbasetags.h>
  150.  
  151. /* proto for special AmiTCP utility funcion; see _chkufb.c */
  152. extern long _install_AmiTCP_callback(void);
  153.  
  154.  
  155. /* global h_errno */
  156. int h_errno = 0;
  157.  
  158. struct Library *UserGroupBase = NULL;
  159. struct Library *SocketBase = NULL;
  160.  
  161. int checkusergrouplib(void)
  162. {
  163.     if(!UserGroupBase)
  164.     {
  165.         if(!(UserGroupBase=OpenLibrary("usergroup.library",4)))
  166.         {
  167.             PyErr_SetString(PyExc_SystemError, "Couldn't open usergroup.library");
  168.             return 0;
  169.         }
  170.     }
  171.     return 1;
  172. }
  173.  
  174. int checksocketlib(void)
  175. {
  176.     if(!SocketBase)
  177.     {
  178.         if(SocketBase=OpenLibrary("bsdsocket.library",4))
  179.         {
  180.             /*
  181.              * Succesfull. Now tell bsdsocket.library:
  182.              * - the address of our errno
  183.              * - the address of our h_errno
  184.              * - our program name
  185.              */
  186.             SocketBaseTags(SBTM_SETVAL(SBTC_ERRNOPTR(sizeof(errno))), &errno,
  187.                            SBTM_SETVAL(SBTC_HERRNOLONGPTR), &h_errno,
  188.                            SBTM_SETVAL(SBTC_LOGTAGPTR), "Python",
  189.                            TAG_END);
  190.         }
  191.         else
  192.         {
  193.             PyErr_SetString(PyExc_SystemError, "Couldn't open bsdsocket.library (start AmiTCP)");
  194.             return 0;
  195.         }
  196.     }
  197.     return 1;
  198. }
  199.  
  200. #endif
  201.  
  202. #ifdef _AMIGA
  203. BOOL from_WB = FALSE;    /* not static! getpath.c needs it! */
  204. static void AmigaCleanup(void)
  205. {
  206. #ifdef AMITCP
  207.     if(UserGroupBase)
  208.     {
  209.         CloseLibrary(UserGroupBase);
  210.         UserGroupBase=NULL;
  211.     }
  212.     if(SocketBase)
  213.     {
  214.         CloseLibrary(SocketBase);
  215.         SocketBase=NULL;
  216.     }
  217. #endif
  218. #ifdef INET225
  219.     if(SockBase)
  220.     {
  221.         cleanup_sockets();
  222.         CloseLibrary(SockBase);
  223.         SockBase = NULL;
  224.     }
  225. #endif
  226.     if(from_WB) Delay(112); // small exit delay before closing WB window
  227. }
  228. #endif
  229.  
  230.  
  231. /* Main program */
  232.  
  233. DL_EXPORT(int)
  234. Py_Main(argc, argv)
  235.     int argc;
  236.     char **argv;
  237. {
  238.     int c;
  239.     int sts;
  240.     char *command = NULL;
  241.     char *filename = NULL;
  242.     FILE *fp = stdin;
  243.     char *p;
  244.     int inspect = 0;
  245.     int unbuffered = 0;
  246.     int skipfirstline = 0;
  247.     int stdin_is_interactive = 0;
  248.  
  249. #ifdef _AMIGA
  250.     if(argc == 0)
  251.     {
  252.         /* Invoked from WorkBench... use WorkBench arguments  */
  253.         /* Make sure <dos.h> was included earlier in order to */
  254.         /* declare _WBArgc and _WBArgv.                       */
  255.         argc = _WBArgc;
  256.         argv = _WBArgv;
  257.         from_WB = TRUE;
  258.     }
  259.  
  260.     atexit(AmigaCleanup);   /* cleanup func */
  261. //  setbuf(stderr,NULL);    /* set error output UNBUFFERED */
  262.  
  263. #endif
  264.  
  265.     orig_argc = argc;    /* For Py_GetArgcArgv() */
  266.     orig_argv = argv;
  267.  
  268.     if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
  269.         inspect = 1;
  270.     if ((p = getenv("PYTHONUNBUFFERED")) && *p != '\0')
  271.         unbuffered = 1;
  272.  
  273.     while ((c = getopt(argc, argv, "c:diOStuvxX")) != EOF) {
  274.         if (c == 'c') {
  275.             /* -c is the last option; following arguments
  276.                that look like options are left for the
  277.                the command to interpret. */
  278.             command = malloc(strlen(optarg) + 2);
  279.             if (command == NULL)
  280.                 Py_FatalError(
  281.                    "not enough memory to copy -c argument");
  282.             strcpy(command, optarg);
  283.             strcat(command, "\n");
  284.             break;
  285.         }
  286.         
  287.         switch (c) {
  288.  
  289.         case 'd':
  290.             Py_DebugFlag++;
  291.             break;
  292.  
  293.         case 'i':
  294.             inspect++;
  295.             Py_InteractiveFlag++;
  296.             break;
  297.  
  298.         case 'O':
  299.             Py_OptimizeFlag++;
  300.             break;
  301.  
  302.         case 'S':
  303.             Py_NoSiteFlag++;
  304.             break;
  305.  
  306.         case 't':
  307.             Py_TabcheckFlag++;
  308.             break;
  309.  
  310.         case 'u':
  311.             unbuffered++;
  312.             break;
  313.  
  314.         case 'v':
  315.             Py_VerboseFlag++;
  316.             break;
  317.  
  318.         case 'x':
  319.             skipfirstline = 1;
  320.             break;
  321.  
  322.         case 'X':
  323.             Py_UseClassExceptionsFlag = 0;
  324.             break;
  325.  
  326.         /* This space reserved for other options */
  327.  
  328.         default:
  329.             fprintf(stderr, usage_line, argv[0]);
  330.             fprintf(stderr, usage_top);
  331.             fprintf(stderr, usage_mid);
  332.             fprintf(stderr, usage_bot,
  333.                 DELIM, DELIM, PYTHONHOMEHELP);
  334. #ifdef _AMIGA
  335.             fprintf(stderr,"Python and Python programs can also be started from the Workbench,\ntooltypes will be converted to command-line arguments.\n");
  336. #endif
  337.             exit(2);
  338.             /*NOTREACHED*/
  339.  
  340.         }
  341.     }
  342.  
  343.     if (command == NULL && optind < argc &&
  344.         strcmp(argv[optind], "-") != 0)
  345.     {
  346.         filename = argv[optind];
  347.         if (filename != NULL) {
  348.             if ((fp = fopen(filename, "r")) == NULL) {
  349.                 fprintf(stderr, "%s: can't open file '%s'\n",
  350.                     argv[0], filename);
  351.                 exit(2);
  352.             }
  353.             else if (skipfirstline) {
  354.                 char line[256];
  355.                 fgets(line, sizeof line, fp);
  356.             }
  357.         }
  358.     }
  359.  
  360.     stdin_is_interactive = Py_FdIsInteractive(stdin, (char *)0);
  361.  
  362.     if (unbuffered) {
  363. #ifdef MS_WINDOWS
  364.         _setmode(fileno(stdin), O_BINARY);
  365.         _setmode(fileno(stdout), O_BINARY);
  366. #endif
  367. #ifndef MPW
  368. #ifdef HAVE_SETVBUF
  369.         setvbuf(stdin,  (char *)NULL, _IONBF, BUFSIZ);
  370.         setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  371.         setvbuf(stderr, (char *)NULL, _IONBF, BUFSIZ);
  372. #else /* !HAVE_SETVBUF */
  373.         setbuf(stdin,  (char *)NULL);
  374.         setbuf(stdout, (char *)NULL);
  375.         setbuf(stderr, (char *)NULL);
  376. #endif /* !HAVE_SETVBUF */
  377. #else /* MPW */
  378.         /* On MPW (3.2) unbuffered seems to hang */
  379.         setvbuf(stdin,  (char *)NULL, _IOLBF, BUFSIZ);
  380.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  381.         setvbuf(stderr, (char *)NULL, _IOLBF, BUFSIZ);
  382. #endif /* MPW */
  383.     }
  384.     else if (Py_InteractiveFlag) {
  385. #ifdef MS_WINDOWS
  386.         /* Doesn't have to have line-buffered -- use unbuffered */
  387.         /* Any set[v]buf(stdin, ...) screws up Tkinter :-( */
  388.         setvbuf(stdout, (char *)NULL, _IONBF, BUFSIZ);
  389. #else /* !MS_WINDOWS */
  390. #ifdef HAVE_SETVBUF
  391.         setvbuf(stdin,  (char *)NULL, _IOLBF, BUFSIZ);
  392.         setvbuf(stdout, (char *)NULL, _IOLBF, BUFSIZ);
  393. #endif /* HAVE_SETVBUF */
  394. #endif /* !MS_WINDOWS */
  395.         /* Leave stderr alone - it should be unbuffered anyway. */
  396.       }
  397.  
  398.     Py_SetProgramName(argv[0]);
  399.     Py_Initialize();
  400.  
  401. #ifdef AMITCP
  402.     /** Sadly, this function cannot be called as a SAS/C standard **/
  403.     /** constructor function. It needs to have the interpreter **/
  404.     /** initialised because it uses the Python Error functions... **/
  405.     (void)_install_AmiTCP_callback();
  406. #endif
  407.  
  408.     if (Py_VerboseFlag ||
  409.         (command == NULL && filename == NULL && stdin_is_interactive))
  410.         fprintf(stderr, "Python %s on %s\n%s\n",
  411.             Py_GetVersion(), Py_GetPlatform(), Py_GetCopyright());
  412.     
  413.     
  414.     if (command != NULL) {
  415.         /* Backup optind and force sys.argv[0] = '-c' */
  416.         optind--;
  417.         argv[optind] = "-c";
  418.     }
  419.  
  420.     PySys_SetArgv(argc-optind, argv+optind);
  421.  
  422.     if ((inspect || (command == NULL && filename == NULL)) &&
  423.         isatty(fileno(stdin))) {
  424.         PyObject *v;
  425.         v = PyImport_ImportModule("readline");
  426.         if (v == NULL)
  427.             PyErr_Clear();
  428.         else
  429.             Py_DECREF(v);
  430.     }
  431.  
  432.     if (command) {
  433.         sts = PyRun_SimpleString(command) != 0;
  434.         free(command);
  435.     }
  436.     else {
  437.         if (filename == NULL && stdin_is_interactive) {
  438.             char *startup = getenv("PYTHONSTARTUP");
  439.             if (startup != NULL && startup[0] != '\0') {
  440.                 FILE *fp = fopen(startup, "r");
  441.                 if (fp != NULL) {
  442.                     (void) PyRun_SimpleFile(fp, startup);
  443.                     PyErr_Clear();
  444.                     fclose(fp);
  445.                 }
  446.             }
  447.         }
  448.         sts = PyRun_AnyFile(
  449.             fp,
  450.             filename == NULL ? "<stdin>" : filename) != 0;
  451.         if (filename != NULL)
  452.             fclose(fp);
  453.     }
  454.  
  455.     if (inspect && stdin_is_interactive &&
  456.         (filename != NULL || command != NULL))
  457.         sts = PyRun_AnyFile(stdin, "<stdin>") != 0;
  458.  
  459.     Py_Finalize();
  460.     return sts;
  461. }
  462.  
  463.  
  464. /* Make the *original* argc/argv available to other modules.
  465.    This is rare, but it is needed by the secureware extension. */
  466.  
  467. void
  468. Py_GetArgcArgv(argc, argv)
  469.     int *argc;
  470.     char ***argv;
  471. {
  472.     *argc = orig_argc;
  473.     *argv = orig_argv;
  474. }
  475.